package com.example.websocket_service.serviceImp;

import com.example.websocket_service.dao.MessageDao;
import com.example.websocket_service.entity.Message;
import com.example.websocket_service.entity.vo.MessageRecordV;
import com.example.websocket_service.entity.form.UserServiceF;
import com.example.websocket_service.entity.vo.PageBean;
import com.example.websocket_service.service.MessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.List;
import java.util.stream.Collectors;

import static com.example.websocket_service.config.FtpUtil.*;

@Service
@Slf4j
public class MessageServiceImp implements MessageService {

    @Autowired
    private MessageDao messageDao;

    @Override
    public void insert(Message message) {
        messageDao.insert(message);
    }

    @Override
    public List<Message> getMessages(String userId) {
        return messageDao.getMessages(userId);
    }

    @Override
    public PageBean<MessageRecordV> getServiceList(UserServiceF form) {
        PageHelper.startPage(form.getCurPage(), form.getPageSize(),true);
        List<MessageRecordV> list = messageDao.getServiceList(form);
//        PageHelper.clearPage();
        PageBean<MessageRecordV> pageBean = new PageBean<>(list);
        List<MessageRecordV> recordVList = list.stream().map(x -> {
            if (2==(x.getType())) {
                try {
                    FTPClient ftpClient = new FTPClient();
                    // 2、指定服务器地址（端口）
                    ftpClient.connect(host, port);
                    // 3、指定账号和密码
                    ftpClient.login(username, password);
                    int reply = ftpClient.getReplyCode();
                    // 如果reply返回230表示成功，如果返回530表示无密码或用户名错误或密码错误或用户权限问题。
                    log.info("ftp连接成功: "+reply);
                    if (!FTPReply.isPositiveCompletion(reply)) {
                        ftpClient.disconnect();
                        log.info("ftp连接失败");
                    }
                    // 转移到FTP服务器目录
                    ftpClient.changeWorkingDirectory(remotePath);
                    FileInputStream input;
                    File file=new File(x.getContent());
//                    if (file.exists()) {
//                        OutputStream is = new FileOutputStream(file);
//                        ftpClient.retrieveFile(x.getContent(), is);
//                        is.close();
//                        input = new FileInputStream(file);
//                        byte bytes[] = new byte[(int) file.length()];
//                        input.read(bytes);
//                        x.setPicData(bytes);
//                        input.close();
//                        ftpClient.logout();
//                    }
                    if (file.exists()) {
                        //通过字节的方式写数据到文件中
                        OutputStream is = new FileOutputStream(file);
                        ftpClient.retrieveFile(x.getContent(), is);
                        is.close();
                        //通过字节的方式读取文件
                        input = new FileInputStream(file);
                        System.out.println(file.length());
                        byte bytes[] = new byte[(int) file.length()];
                        input.read(bytes);
                        x.setPicData(bytes);
                        input.close();
                        ftpClient.logout();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return x;
        }).collect(Collectors.toList());
//        return recordVList;
        pageBean.setList(recordVList);
        return pageBean;
    }

    @Override
    public void updateUrl(String userId, String url) {
        messageDao.updateUrl(userId, url);
    }

    @Override
    public void delete(Long id) {
        messageDao.delete(id);
    }
}
